package com.piesat.quake.business.device.mqtt.param;

import com.piesat.quake.business.device.mqtt.DataType;
import com.piesat.quake.business.device.mqtt.NumberType;

import java.lang.reflect.Field;

import static com.piesat.quake.business.device.mqtt.reader.NumberReader.LITTLE_ENDIAN;

public class NumberParseParam extends ParseParam {
    /**
     * 大小头，0：大头 1：小头
     */
    public Integer endian = 1;
    /**
     * 数据类型
     * 1：
     */
    public Integer numberType;

    public NumberParseParam() {
    }

    public NumberParseParam(Field field, Integer type, Integer endian, Integer numberType) {
        super(field, type);
        this.endian = endian;
        this.numberType = numberType;
    }

    public static NumberParseParam create(Field field, Integer numberType) {
        return new NumberParseParam(field, DataType.NUMBER.ordinal(), LITTLE_ENDIAN, numberType);
    }

    public static NumberParseParam createByte(Field field) {
        return new NumberParseParam(field, DataType.NUMBER.ordinal(), LITTLE_ENDIAN, NumberType.BYTE);
    }

    public static NumberParseParam createShort(Field field) {
        return new NumberParseParam(field, DataType.NUMBER.ordinal(), LITTLE_ENDIAN, NumberType.SHORT);
    }

    public static NumberParseParam createInt(Field field) {
        return new NumberParseParam(field, DataType.NUMBER.ordinal(), LITTLE_ENDIAN, NumberType.INT);
    }

    public static NumberParseParam createLong(Field field) {
        return new NumberParseParam(field, DataType.NUMBER.ordinal(), LITTLE_ENDIAN, NumberType.LONG);
    }

    public static NumberParseParam createDouble(Field field) {
        return new NumberParseParam(field, DataType.NUMBER.ordinal(), LITTLE_ENDIAN, NumberType.DOUBLE);
    }

    public static NumberParseParam createFloat(Field field) {
        return new NumberParseParam(field, DataType.NUMBER.ordinal(), LITTLE_ENDIAN, NumberType.FLOAT);
    }

    public Integer getEndian() {
        return endian;
    }

    public void setEndian(Integer endian) {
        this.endian = endian;
    }

    public Integer getNumberType() {
        return numberType;
    }

    public void setNumberType(Integer numberType) {
        this.numberType = numberType;
    }

    @Override
    public int getLength() {
        switch (numberType) {
            case NumberType.BYTE:
                return 1;
            case NumberType.INT:
            case NumberType.FLOAT:
                return 4;
            case NumberType.SHORT:
                return 2;
            case NumberType.LONG:
            case NumberType.DOUBLE:
                return 8;
        }
//        if (type == 1) {//byte
//            return 1;
//        } else if (type == 2) {//int
//            return 4;
//        }else if (type == 3) {//float
//            return 4;
//        }else if (type == 4) {//short
//            return 2;
//        }else if (type == 5) {//long
//            return 8;
//        }else if (type == 6) {//double
//            return 8;
//        }
        return -1;
    }

    @Override
    public String toString() {
        return "NumberParseParam{" +
                "endian=" + endian +
                ", numberType=" + numberType +
                ", field='" + field + '\'' +
                ", type=" + type +
                '}';
    }
}